package com.gold.security;

import com.alibaba.fastjson.JSONObject;
import com.gold.common.CacheUtils;
import com.gold.common.JsonUtils;
import com.gold.common.TokenUtil;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**退出成功自定义
 * @author XiaoHao
 * @date 2022-05-09 15:20
 */
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {
    @Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        /**
         * 退出成功将token加入黑名单
         */
        String token = request.getHeader("Token");
        CacheUtils.setList(token);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write(JsonUtils.responseJson(true,200,"注销成功","").toJSONString());
        out.flush();
        out.close();
    }
}
